package com.dream.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;


public class DianyuanFragment extends android.app.Fragment implements View.OnClickListener {

    private ImageView dianyuanPower;
    private int powerState=0;

    private MainActivity mainActivity;
    private SDKManager sdkManager;

    public DianyuanFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_dianyuan, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        dianyuanPower = (ImageView) view.findViewById(R.id.dianyuan_power);
    }

    public void setOnClick(){
        dianyuanPower.setOnClickListener(this);
    }

    public void controlDianYuan(Integer state){
        if (sdkManager.getLandingSate()){
            sdkManager.ControlDev(DevUIIDConfig.power,state);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dianyuan_power:
                if (powerState==0){
                    dianyuanPower.setImageResource(R.drawable.bg_kongtiao_on);
                    powerState++;
                    //开启
                    controlDianYuan(1);
                }else {
                    dianyuanPower.setImageResource(R.drawable.bg_kongtiao_off);
                    powerState--;
                    //关闭
                    controlDianYuan(0);
                }
                break;
        }
    }
}
